﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EFCoreRepository.DbContexts;
using Zoomtel.Entity.BIZ;
using Zoomtel.Persist.BIZ;
using Zoomtel.Persist.BIZ.Models;

namespace Zoomtel.Service.BIZ
{
    public class CashLogService
    {
        private CashLogRepository _cashLogRepository;
        private AgentInfoRepository _agentInfoRepository;
        private AgentInfoService _agentInfoService;
        private DefaultDbContext _defaultDbContext;
        public CashLogService(CashLogRepository cashLogRepository, AgentInfoRepository agentInfoRepository, AgentInfoService agentInfoService, DefaultDbContext defaultDbContext)
        {
            _cashLogRepository = cashLogRepository;
            _agentInfoRepository = agentInfoRepository;
            _agentInfoService = agentInfoService;
            _defaultDbContext = defaultDbContext;
        }

        public async Task<IResultModel> Query(CashLogQueryModel model)
        {
            var result = await _cashLogRepository.Query(model);

            return ResultModel.Success(result);
        }

        public async Task<IResultModel> Update(CashLogEntity model)
        {
            var result = new ResultModel<int>();

            int i = _cashLogRepository.Update(model);
            if (i > 0)
            {

                return result.Success(i);
            }
            else
            {
                return result.Failed();

            }

        }

        public async Task<IResultModel> Payment(CashLogEntity model)
        {
            var result = new ResultModel<int>();

            _cashLogRepository.BeginTrans();
            await Update(model);
            
            var agent = _agentInfoRepository.FindEntity(model.Uid);
            agent.LockBalance = agent.LockBalance - model.Money;
            int i = _agentInfoRepository.Update(agent);

            //int i = _cashLogRepository.Update(model);
            if (i > 0)
            {
                _cashLogRepository.Commit();
                return result.Success(i);
            }
            else
            {
                _cashLogRepository.Rollback();
                return result.Failed();

            }

        }

        public async Task<IResultModel> Audit(string ids, string status)
        {
            List<string> idList = ids.Split(',').ToList();
            var result = await _cashLogRepository.FindListAsync(a => idList.Contains(a.Id));
            int success = 0;
            _cashLogRepository.BeginTrans();
            foreach (var item in result)
            {
                var logStatus = item.Status;
                if (logStatus!="0") {
                    _cashLogRepository.Rollback();
                    return ResultModel.Failed("提现记录的审核状态不正确！");
                }
                item.Status = status;
                item.PassTime = DateTime.Now;
                success = success + _cashLogRepository.Update(item);
                if ("20".Equals(status)) {//驳回需要回滚lockmoney
                    var cashlog = _cashLogRepository.FindEntity(item.Id);
                    //var money = cashlog.Money;
                    //var agent = _agentInfoRepository.FindEntity(cashlog.Uid);
                    //agent.AccountBalance = agent.AccountBalance + money;
                    //agent.LockBalance = agent.LockBalance - money;
                    //_agentInfoRepository.Update(agent);
                    await _agentInfoService.ChangeBalance(cashlog.Uid,ChangeType.UserCarryRollBack.ToString(), cashlog.Money,cashlog.Id,"提现驳回", _defaultDbContext.LoginInfo.LoginName);
                }
            }
            if (success > 0)
            {
                _cashLogRepository.Commit();
                return new ResultModel<int>().Success(success);
            }
            else
            {
                _cashLogRepository.Rollback();
                return new ResultModel<int>().Failed();
            }
        }
    }
}
